import json
import datasets
from datasets import DownloadManager,DatasetInfo

class Round1TestDataset(datasets.GeneratorBasedBuilder):
    def _info(self):
        return DatasetInfo(
            description = """
                这是一个dataset表示我参加的第一轮比赛
                """,
            # 指定该数据集的字段信息和类型(最后要处理成的形式)
            features=datasets.Features(
                {
                    # "id": datasets.Value("string"),
                    "problem": datasets.Value("string"),
                    "question": datasets.Value("string"),
                    # "options": datasets.features.ClassLabel(names=["0", "1"]),
                    # "options": datasets.features.Sequence(datasets.Value("string")),
                    "options": datasets.Value("string"),
                    "answer":datasets.Value("string")
                }
            )
        )
    def _split_generators(self, dl_manager: DownloadManager):
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepath": "./round1_train_data.jsonl",
                }
            )
        ]
    def _generate_examples(self, filepath):
        filepath = "round1_train_data.jsonl"
        with open(filepath, "r", encoding="utf-8") as f:
                data = f.readlines()
                data = [i.strip() for i in data]
                id = 0
                for i in data:
                    item  = json.loads(i,encoding="utf-8")
                    problem = item["problem"]
                    for question in item["questions"]:
                        print(question)
                        id +=1
                        options = question["options"]
                        handle_options = []
                        for  i in range(len(options)):
                            if i == 0 :
                                  handle_options.append("A:"+options[i])
                            if i == 1 :
                                  handle_options.append("B:"+options[i])
                            if i == 2 :
                                  handle_options.append("C:"+options[i])
                            if i == 3 :
                                  handle_options.append("D:"+options[i])

                        new_options = " ".join(handle_options)
                        yield id, {
                            # "id": item["id"],
                            "problem": problem,
                            "question": question["question"],
                            "options": new_options,
                            "answer": question["answer"]
                        }